package com.carkeeper.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carkeeper.dto.ClassPropertiesDTO;
import com.carkeeper.dto.ComponentClassDTO;
import com.carkeeper.exception.BussinessException;
import com.carkeeper.pojo.ClassPropertiesPO;
import com.carkeeper.mapper.ClassPropertiesMapper;
import com.carkeeper.mapper.CompTypeMapper;
import com.carkeeper.service.ClassPropertiesService;
import com.carkeeper.vo.Pager;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ClassPropertiesServiceImpl extends ServiceImpl<ClassPropertiesMapper, ClassPropertiesPO> implements ClassPropertiesService {

    @Autowired
    private  CompTypeMapper compTypeMapper;



    @Override

    public String addClassProperties(ClassPropertiesPO classPropertiesPO) {
        save(classPropertiesPO);
        return "新增成功";
    }

    @Override
    public String deleteClassProperties(Long id) {
        if (id == null) {
            return "请输入id";
        }
        boolean success = removeById(id);
        if (!success) {
            return "id不存在";
        }
        return "删除成功";
    }

    @Override
    @Transactional
    public String updateClassProperties(Long id, ClassPropertiesPO classPropertiesPO) {
        // 判断是否包含id
        if (id != null) {
            ClassPropertiesPO properties = getById(id);
            if (properties == null) {
                throw new BussinessException("分类不存在！");
            }
            // 如果有，则修改
            classPropertiesPO.setId(id);
            updateById(classPropertiesPO);
        }
        return "更新分类成功";
    }

    @Override
    public ClassPropertiesDTO getClassPropertiesById(Long id) {
        ClassPropertiesPO properties = getById(id);
        if (properties == null) {
            throw new BussinessException("分类不存在");
        }
        Integer classId = properties.getClassId();
        List<String> compTypeName = compTypeMapper.queryByClassPropertiesId(classId.longValue());
        String name = compTypeName.get(0);
        ComponentClassDTO componentClassDto = new ComponentClassDTO();
        componentClassDto.setId(classId.longValue());
        componentClassDto.setName(name);
        ClassPropertiesDTO classPropertiesDto = new ClassPropertiesDTO();
        BeanUtils.copyProperties(properties, classPropertiesDto);
        classPropertiesDto.setComponentClass(componentClassDto);
        return classPropertiesDto;
    }

    @Override
    public Pager<ClassPropertiesDTO> getClassPropertiesByPage(Integer page, Integer pagesize, Integer classId) {
        Page<ClassPropertiesPO> result = query()
                .eq("class_id", classId)
                .page(new Page<>(page, pagesize));
        List<ClassPropertiesPO> list = result.getRecords();
        List<ClassPropertiesDTO> classPropertiesDTOS = new ArrayList<>();
        for (ClassPropertiesPO classPropertiesPO : list) {
            List<String> strings = compTypeMapper.queryByClassPropertiesId(classId.longValue());
            String name = strings.get(0);
            ComponentClassDTO componentClassDto = new ComponentClassDTO();
            componentClassDto.setId(classId.longValue());
            componentClassDto.setName(name);
            ClassPropertiesDTO classPropertiesDto = new ClassPropertiesDTO();
            BeanUtils.copyProperties(classPropertiesPO, classPropertiesDto);
            classPropertiesDto.setComponentClass(componentClassDto);
            classPropertiesDTOS.add(classPropertiesDto);
        }

        Page<ClassPropertiesDTO> page1 = new Page<>();
        page1.setPages(page);
        page1.setSize(pagesize);
        page1.setRecords(classPropertiesDTOS);
        long counts = result.getTotal();
        long pages = result.getPages();
        Pager<ClassPropertiesDTO> pager = new Pager<>(page1);
        pager.setCounts(counts);
        pager.setPages(pages);
        return pager;
    }
}
